<?php
namespace app\index\model;
use think\Model;

class HongYan extends Model{

    // const MAP_HONGYAN = [
    //     'code' => '编码',
    //     'item_name' => '名称',
    //     'model_no' => '规格型号',
    //     'unit' => '单位',
    //     'price_with_tax' => '含税价格',
    //     'currency_type' => '币种',
    //     'local_currency' => '本币价格',
    //     'price_without_tax' => '不含税价格'
    // ];
    
    /**
     * 获取单条数据
     */
    public function findData($id){
        return HongYan::get($id)->toArray();
    }

    /**
     * 根据code获取最新的单条数据
     */
    public function findByCode($code){
        return db('hong_yan')->where(['code'=>'假的code_null'])->order(['id'=> 'desc'])->find();
    }

    /**
     * 根据code查找memo
     */
    public function findMemoByCode($code){
        $sql = "select * from dev_hong_yan where code = '{$code}' and memo is not null and memo <> '' order by id desc limit 1";
        $memoInfo = db('hong_yan')->query($sql);
        if(!empty($memoInfo)) {
            return $memoInfo[0];
        }else{
            return [];
        }
    }

    /**
     * 获取列表
     */
    public function getList($page, $limit, $code = '', $itemName = '', $beginDate = '', $endDate = '', $modelNo = ''){
        $where = " where 1=1 ";
        $sql = "SELECT a.* FROM dev_hong_yan as a inner join (SELECT MAX(id) as maxId from dev_hong_yan GROUP BY code) as b on a.id = b.maxId";
        // if (!empty($code)){
        //     $where .= " and a.code = '{$code}'";
        // } 
        if (!empty($itemName)) {
            $where .= " and (a.item_name like '%{$itemName}%' or a.code like '%{$itemName}%' or a.model_no like '%{$itemName}%' or a.memo like '%{$itemName}%' ";
            $where .= " or a.buyer like '%{$itemName}%') ";
        }
        // if (!empty($modelNo)) {
        //     $where .= " and a.model_no = '{$modelNo}'";
        // }
        if (!empty($beginDate) && !empty($endDate)) {
            $where .= " and a.ctime between '{$beginDate}' and '{$endDate}'";
        }elseif(!empty($beginDate)) {
            $endDate = time();
            $where .= " and a.ctime between '{$beginDate}' and '{$endDate}'";
        }elseif(!empty($endDate)) {
            $where .= " and a.ctime <= '{$endDate}'";
        }
        $page = ($page - 1)*$limit;
        $limit = " order by id desc limit $page, $limit";
        $sql = $sql.$where.$limit;
        return db('hong_yan')->query($sql);
    }

    // /**
    //  * 获取总数量
    //  */
    // public function count($code = ''){
    //     if(!empty($code)) {
    //         return db('hong_yan')->where(['code'=>$code])->count();
    //     }else{
    //         return db('hong_yan')->count();;
    //     }
    // }

    /**
     * 获取总数量
     */
    public function count($code = '', $itemName = '', $beginDate = '', $endDate = '', $modelNo = ''){
        $where = " where 1=1 ";
        $sql = "SELECT count(*) as count FROM dev_hong_yan as a inner join (SELECT MAX(id) as maxId from dev_hong_yan GROUP BY code) as b on a.id = b.maxId";
        if (!empty($code)){
            $where .= " and a.code = '{$code}'";
        } 
        if (!empty($itemName)) {
            $where .= " and a.item_name = '{$itemName}'";
        }
        if (!empty($modelNo)) {
            $where .= " and a.model_no = '{$modelNo}'";
        }
        if (!empty($beginDate) && !empty($endDate)) {
            $where .= " and a.ctime between '{$beginDate}' and '{$endDate}'";
        }elseif(!empty($beginDate)) {
            $endDate = time();
            $where .= " and a.ctime between '{$beginDate}' and '{$endDate}'";
        }elseif(!empty($endDate)) {
            $where .= " and a.ctime <= '{$endDate}'";
        }
        $sql = $sql.$where;
        $result = db('hong_yan')->query($sql);
        if (!empty($result)) {
            return $result[0]['count'];
        } else {
            return 0;
        }
    }

    /**
     * 保存数据
     */
    public function saveData($param, $uname){
        if (!empty($param['id'])) {
            $hongYan = HongYan::get($param['id']);
            if (empty($hongYan)) {
                throw Exception('没有查询到数据');
            } 
            $logModel = new ActionLog();
            $oldArray = $newArray = $hongYan->toArray();
            foreach($param as $key => $val) {
                $newArray[$key] = $val;
            }
            $diff = array_diff_assoc($oldArray, $newArray);
            //数据有差异，变更修改时间
            if (!empty($diff)) {
                $hongYan->mtime = time();
            }
            $logModel->insertLog($oldArray, $newArray, HYJG, $uname);    
        } else {
            $hongYan = $this;
            $hongYan->ctime = time();
            $hongYan->mtime = time();
        }
        $hongYan->code = $param['code'];
        $hongYan->item_name = $param['item_name'];
        if(isset($param['memo'])) {
            $hongYan->memo = $param['memo'];
        }
        if(isset($param['buyer'])) {
            $hongYan->buyer = $param['buyer'];
        }
        $hongYan->model_no = $param['model_no'];
        $hongYan->unit = $param['unit'];
        $hongYan->price_with_tax = $param['price_with_tax'];
        $hongYan->currency_type = $param['currency_type'];
        $hongYan->local_currency = $param['local_currency'];
        $hongYan->price_without_tax = $param['price_without_tax'];
        $hongYan->uname = $uname;
        $hongYan->save();
        if (!empty($param['id'])) {
            return HongYan::get($param['id']);
         }
        return $hongYan;

    }
    
}
?>